package com.lightsaber.trade.as.service.device;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.lightsaber.trade.as.dao.device.DeviceDao;
import com.lightsaber.trade.as.dao.device.InfoCollectForDeviceDao;
import com.lightsaber.trade.as.entity.device.Device;
import com.lightsaber.trade.as.entity.device.InfoCollectForDevice;
import com.lightsaber.trade.core.dao.BaseDao;
import com.lightsaber.trade.core.service.BaseServiceImpl;

@Service
@Transactional
public class InfoCollectForDeviceManager extends BaseServiceImpl<InfoCollectForDevice, String> {

    @Autowired
    InfoCollectForDeviceDao infoCollectForDeviceDao;
    @Autowired
    DeviceDao deviceDao;

    @Override
    protected BaseDao<InfoCollectForDevice, String> getDao() {
        return infoCollectForDeviceDao;
    }

    public List<String> findNoReadInfo(String deviceId) throws InterruptedException {
        Device device = deviceDao.get(deviceId);
        List<InfoCollectForDevice> noReadInfos = infoCollectForDeviceDao.findNoReadInfo(device);

        List<String> infoList = new ArrayList<String>();

        for (InfoCollectForDevice infoCollectForDevice : noReadInfos) {
            infoList.add("(Device:" + device.getName() + ") " + infoCollectForDevice.getContent());

            infoCollectForDevice.setStatus("Read");
            infoCollectForDeviceDao.save(infoCollectForDevice);
        }

        return infoList;
    }

    public void save(String deviceSerialNumber, String content) {
        Device device = deviceDao.findUniqueBy("serialNumber", deviceSerialNumber);

        InfoCollectForDevice infoCollectForDevice = new InfoCollectForDevice();
        infoCollectForDevice.setDevice(device);
        infoCollectForDevice.setContent(content);
        infoCollectForDevice.setStatus("NoRead");

        infoCollectForDeviceDao.save(infoCollectForDevice);
    }

}
